package com.hzncc.flowable_diboot.contract.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.diboot.core.controller.BaseCrudRestController;
import com.diboot.core.util.V;
import com.diboot.core.vo.JsonResult;
import com.diboot.core.vo.Pagination;
import com.diboot.iam.annotation.BindPermission;
import com.diboot.iam.annotation.Log;
import com.diboot.iam.annotation.OperationCons;
import com.hzncc.flowable_diboot.contract.dto.ContractAuditCancelDTO;
import com.hzncc.flowable_diboot.contract.entity.ContractAudit;
import com.hzncc.flowable_diboot.contract.entity.ContractAuditCancel;
import com.hzncc.flowable_diboot.contract.service.ContractAuditCancelService;
import com.hzncc.flowable_diboot.contract.service.ContractAuditService;
import com.hzncc.flowable_diboot.contract.vo.ContractAuditCancelDetailVO;
import com.hzncc.flowable_diboot.contract.vo.ContractAuditCancelListVO;
import com.hzncc.flowable_diboot.enums.ContractAuditStatusEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
* 合同取消 相关rest接口
* @author MyName
* @version 1.0
* @date 2024-08-20
* Copyright © MyCorp
*/
@Slf4j
@RequestMapping("/contract-audit-cancel")
@BindPermission(name = "合同取消")
@RestController
public class ContractAuditCancelController extends BaseCrudRestController<ContractAuditCancel> {

    @Autowired
    private ContractAuditCancelService contractAuditCancelService;

    @Autowired
    private ContractAuditService contractAuditService;

    /**
    * 查询资源对象的列表VO记录
    * <p>
    * url请求参数示例: ?fieldA=abc&pageSize=20&pageIndex=1&orderBy=id
    * </p>
    * @return
    * @throws Exception
    */
    @Log(operation = OperationCons.LABEL_LIST)
    @BindPermission(name = OperationCons.LABEL_LIST, code = OperationCons.CODE_READ)
    @GetMapping()
    public JsonResult<List<ContractAuditCancelListVO>> getListVOMapping(ContractAuditCancelDTO queryDto, Pagination pagination) throws Exception {
        LambdaQueryChainWrapper<ContractAudit> lq = contractAuditService.lambdaQuery();
        lq.select(ContractAudit::getId)
                .ne(ContractAudit::getContractStatus, ContractAuditStatusEnum.HISTORY.getCode())
                .ne(ContractAudit::getContractStatus, ContractAuditStatusEnum.NO_SUBMIT.getCode())
                .eq(ContractAudit::getAuditType,"cancel");
        List<ContractAudit> entityList = contractAuditService.getEntityList(lq);
        QueryWrapper<ContractAuditCancelDTO> queryWrapper = super.buildQueryWrapperByDTO(queryDto);
        if (pagination != null && V.isEmpty(pagination.getOrderBy())) {
            pagination.setOrderBy(Pagination.ORDER_BY_ID_DESC);
        }
        if (entityList != null && !entityList.isEmpty()) {
            queryWrapper.lambda().in(ContractAuditCancel::getContractAuditId, entityList.stream().map(ContractAudit::getId).collect(Collectors.toList()));
        }
        queryWrapper.select("contract_audit_id");
        List<ContractAuditCancel> voList = this.getService().getEntityList(queryWrapper, pagination);
        return contractAuditService.selectContractCancelDetailList(voList).bindPagination(pagination);
    }

    /**
    * 根据id查询资源对象的详情VO
    * @param id ID
    * @return
    * @throws Exception
    */
    @Log(operation = OperationCons.LABEL_DETAIL)
    @BindPermission(name = OperationCons.LABEL_DETAIL, code = OperationCons.CODE_READ)
    @GetMapping("/{id}")
    public JsonResult<ContractAuditCancelDetailVO> getDetailVOMapping(@PathVariable("id")String id) throws Exception {
        ContractAuditCancelDetailVO detailVO = contractAuditCancelService.getViewObject(id, ContractAuditCancelDetailVO.class);
        return JsonResult.OK(detailVO);
    }


    /**
    * 创建资源对象数据
    * @param contractAuditCancel
    * @return JsonResult
    * @throws Exception
    */
    @Log(operation = OperationCons.LABEL_CREATE)
    @BindPermission(name = OperationCons.LABEL_CREATE, code = OperationCons.CODE_WRITE)
    @PostMapping()
    public JsonResult<?> createModelMapping(@RequestBody ContractAuditCancel contractAuditCancel) throws Exception {
        boolean success = contractAuditCancelService.createEntity(contractAuditCancel);
        log.debug("新建数据 合同取消:{} {}", contractAuditCancel.getId(), success? "成功":"失败");
        return JsonResult.OK(contractAuditCancel.getId());
    }

    /**
    * 批量创建资源对象数据
    * @param contractAuditCancelList
    * @return JsonResult
    * @throws Exception
    */
    @Log(operation = OperationCons.LABEL_BATCH_UPDATE)
    @BindPermission(name = OperationCons.LABEL_UPDATE, code = OperationCons.CODE_WRITE)
    @PostMapping("/batch")
    public JsonResult<?> batchCreateModelMapping(@RequestBody List<ContractAuditCancel> contractAuditCancelList) throws Exception {
        boolean success = contractAuditCancelService.createEntities(contractAuditCancelList);
        log.debug("批量新建数据 合同取消 {}", success? "成功":"失败");
        return JsonResult.OK(success);
    }

    /**
    * 根据id更新资源对象
    * @param contractAuditCancel
    * @return JsonResult
    * @throws Exception
    */
    @Log(operation = OperationCons.LABEL_UPDATE)
    @BindPermission(name = OperationCons.LABEL_UPDATE, code = OperationCons.CODE_WRITE)
    @PutMapping("/{id}")
    public JsonResult<?> updateModelMapping(@PathVariable("id")String id, @RequestBody ContractAuditCancel contractAuditCancel) throws Exception {
        boolean success = contractAuditCancelService.updateEntity(contractAuditCancel);
        log.debug("更新数据 合同取消:{} {}", id, success? "成功":"失败");
        return JsonResult.OK(success);
    }

    /**
    * 根据id删除资源对象
    * @param id
    * @return
    * @throws Exception
    */
    @Log(operation = OperationCons.LABEL_DELETE)
    @BindPermission(name = OperationCons.LABEL_DELETE, code = OperationCons.CODE_WRITE)
    @DeleteMapping("/{id}")
    public JsonResult<?> deleteModelMapping(@PathVariable("id") String id) throws Exception {
        boolean success = contractAuditCancelService.deleteEntity(id);
        log.debug("删除数据 合同取消:{} {}", id, success? "成功":"失败");
        return JsonResult.OK(success);
    }

    /**
    * 根据ids集合删除资源对象
    * @param ids
    * @return
    * @throws Exception
    */
    @Log(operation = OperationCons.LABEL_BATCH_DELETE)
    @BindPermission(name = OperationCons.LABEL_DELETE, code = OperationCons.CODE_WRITE)
    @PostMapping("/batch-delete")
    public JsonResult<?> deleteEntityMapping(@RequestBody List<String> ids) throws Exception {
        boolean success = contractAuditCancelService.deleteEntities(ids);
        log.debug("批量删除数据 合同取消:{} {}", ids, success? "成功":"失败");
        return JsonResult.OK(success);
    }
}